<?php

require_once 'Zend/View.php';
require_once 'Zend/Controller/Front.php';

class Core_Widget_Nform extends Core_Widget_Abstract
{
	protected $_data;
	//array('title' => 'dsad', 'headtext' => 'dasd');
	protected $_transform = array();
	//array('content' => 'headtext');
	protected $_message = array();
	//array('content' => 'zzzzzzz');
	
	public function isProcess()
	{
		return ($this->getRequest()->getPost('formid') == get_class($this));
	}
	public function render($v = 'render')
	{
		$view = $this->getView();
		
		$view->formid = get_class($this);
		$view->rules = $this->rules();
		$view->data &= $this->_data;
		$view->message &= $this->_message;
		$view->transform &= $this->_transform;
		
		return $view->render($v.'.phtml');
	}

	public function validate()
	{
		$value = $this->getData();
		
		foreach($this->rules() as $k => $rules)
		if(@is_array($rules['valid']))
		{
			$validators = new Zend_Validate();
			foreach($rules['valid'] as $rule)
				$validators->addValidator($rule);
			
			if(!$validators->isValid($value->{$this->getTransform($k)}))
			{
				$this->_messages[$this->getTransform($k)] = $validators->getMessages();
			}
		}
	}
	public function filter($key = null)
  {
		$value = $this->getData();
		$rules = $this->rules();
		
		if(is_null($key))
		{
			$data = array();
			foreach($rules as $key => $rule)
			{
				$key 				= $this->getTransform($key);
				$data[$key] = $value->{$key};
				if(@is_array($rule['filter']))
				{
					foreach($rule['filter'] as $filter)
						$data[$key] = $filter->filter($data[$key]);
				}
			}
		}
		else
		{
			$data					= $value->{$key};
			$key 					= $this->getTransform($key);
			if(@is_array($rules[$key]['filter']))
			{
				foreach($rules[$key]['filter'] as $filter)
					$data			= $filter->filter($data);
			}
		}
		return $data;
  }

  public function getData()
  {
    return @$this->_data;
  }
  public function setData($data)
  {
		$this->_data = is_array($data)?((object)$data):$data;
		return $this;
  }
  public function clearData()
  {
		unset($this->_data);
  	return $this;
  }

  public function hasMessage($index = null)
  {
    $messages = $this->_message;

    if($index === null)
    {
      return empty($messages)?false:true;
    }
    return empty($messages[$index])?false:true;
  }
  public function getMessage($index = null)
  {
    return ($index === null)?$this->_message:@$this->_message[$index];
  }
  public function setMessage($key, $val = null)
  {
    $stores   &= $this->_message;
    $messages = is_array($key)?$key:array($key=>$val);
    
    foreach($messages as $k=>$v)
      $stores[$k] = $v;

    return $this;
  }
  public function clearMessage()
  {
   $this->_message = array(); 
   return $this;
  }

  public function getTransform($index = null)
  {
		if($index === null)
			return $this->transform;
		
    return empty($this->_transform[$index])?$index:$this->_transform[$index];
  }
  public function setTransform($transform)
  {
		$this->_transform = $transform;
		return $this;
  }
  public function clearTransform()
  {
		unset($this->_transform);
  	return $this;
  }
}